<template>
  <div class="login">
    <el-form class="box" :model="form" :rules="rules" ref="form">
      <div class="logo">
        <img src="@/assets/common/login-logo.png" alt="" />
      </div>
      <el-form-item prop="mobile">
        <el-input v-model="form.mobile"></el-input>
      </el-form-item>

      <el-form-item prop="password">
        <el-input v-model="form.password" type="password"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" class="btn" @click="submit">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// import { login } from '@/api/user.js'
export default {
  data() {
    return {
      form: {
        mobile: "13800000002",
        password: "123456",
      },
      rules: {
        mobile: [
          { required: true, message: "必填", trigger: "change" },
          {
            validator: (rule, value, callback) => {
              const reg =
                /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/;
              if (reg.test(value)) {
                callback();
              } else {
                callback(new Error("请输入正确手机号"));
              }
            },
          },
        ],
        password: [
          { required: true, message: "必填", trigger: "change" },
          {
            min: 6,
            max: 12,
            message: "请输入6-12为密码",
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    submit() {
      this.$refs.form.validate(async (result) => {
        if (result) {
          // const res = await login(this.form)
          await this.$store.dispatch("user/toLogin", this.form);
          this.$message.success("验证成功");
          // this.$router.push('/')
          const redirect = this.$route.query.redisrect || "/";
          this.$router.push(redirect);
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  background: url(~@/assets/common/login.jpg);
  display: flex;
  justify-content: center;
  align-items: center;
  .box {
    width: 520px;
    height: 400px;
    .logo {
      text-align: center;
      padding-bottom: 20px;
    }
    .btn {
      width: 100%;
      padding: 15px;
      background-color: #407ffe;
      border: 0;
    }
  }
}
</style>
